<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>for循环</title>
	<style type="text/css" media="screen">
		#box{
			margin-top: 100px;
			text-align: center;
		}
	</style>
</head>
<body>
	<!-- for循环由四部分组成：
	    初始化（只在for循环开始时执行一次）-测试条件（判断循环是否要继续跑一轮）-动作（每一轮循环重复的代码）-更新（每一轮循环结束后在结尾更新循环变量） -->
          <div id="box">
          	     <img src="" alt="" id="seat1">
          	     <img src="" alt="" id="seat2">
          	     <img src="" alt="" id="seat3">
          	     <img src="" alt="" id="seat4">
          	     <img src="" alt="" id="seat5">
          	     <img src="" alt="" id="seat6">
          	     <img src="" alt="" id="seat7">
          	     <img src="" alt="" id="seat8">
          </div>
</body>
<script type="text/javascript">
// 已知数量37.
	// for(var i=0;i<37;i++){
	// 	takeStep();//循环执行的动作
	// }

	// var count = prompt('Enter a number greater than 0 :' , '10');
	// if(count>0){
	// 	for(var i=count;i>0;i--){
 //                             alert('Start' + i);
 //                             alert("结束")
	// 	}
	// }else{
	// 		alert('无效数据')
	// }
window.onload = function() {
	var seats = [false,true,false,true,true,true,false,true,false];
	var selSeat = -1;
	function initSeats() {
		for(var i=0;i<seats.length;i++) {
		if(seats[i]) {
			alert("seat" + i + "是空位");
			document.getElementById('"seat' + i).src = "rock1.jpg";
			document.getElementById('"seat' + i).alt = "Available seat";
		}else {
			alert("seat" + i + "不是空位");
			document.getElementById('"seat' + i).src = "rock2.jpg";
			document.getElementById('"seat' + i).alt = "Unavailable seat";
		}
	    }
	}
	function findSeat() {
		// 如果座位已经被选中,重新启动所有席位来清除它们。
		if(selSeat>=0) {
			selSeat = -1;
			initSeats();
		}
		// 搜索所有可用性的席位
		for(var i=0;i<seats.length;i++) {
		// 看看当前的座位是可用的
		if(seats[i]) {
			// 设置选择座位和座位更新的外观
			selSeat = i;
			document.getElementById('"seat' + i).src = "rock1.jpg";
			document.getElementById('"seat' + i).alt = "Your seat";
		// 提示用户接受的座位
		var accept = confirm("seat" + (i+1) + "is available.Accept?")
		if(!accept) {
		// 用户拒绝了座位,所以清理座位选择,继续找
		           selSeat = -1;
			document.getElementById('"seat' + i).src = "rock2.jpg";
			document.getElementById('"seat' + i).alt = "Unavailable seat";
		   }
		}
	    }
	}
	findSeat();



	// 连续找三个座位
	for(var i=0;i<seats.length;i++){
		// 看看当前的座位上+下一个可用的两个席位
		if(seats[i]){
			if(seats[i+1]){
				if(seate[i+2]){
					// 设置选择座位和更新外观的席位
					selSeat = i;
					document.getElementById("seat" + i).src = "seat_select.png";
					document.getElementById("seat" +i).alt = "Your seat";
					document.getElementById("seat" + (i+1)).src = "seat_select.png";
					document.getElementById("seat" +(i+1)).alt = "Your seat";
					document.getElementById("seat" + (i+2)).src = "seat_select.png";
					document.getElementById("seat" +(i+2)).alt = "Your seat";
					// 提示用户接受席位
					var accept = confirm("Seats" + (i+1) + "through" + (i+3) + "are available.Accept?");
					if(accept) {
						// 用户接受了座位就终止循环
						break；
					}else{
						// 用户拒绝了座位,所以清理座位选择,继续找
						document.getElementById("seat" + i).src = "seat_avail.png";
						document.getElementById("seat" +i).alt = "Available seat";
						document.getElementById("seat" + (i+1)).src = "seat_avail.png";
						document.getElementById("seat" +(i+1)).alt = "Available seat";
						document.getElementById("seat" + (i+2)).src = "seat_avail.png";
						document.getElementById("seat" +(i+2)).alt = "Available seat";
					}
				}
			}
		}
	}
}
</script>
</html>